package com.app.controller;

import com.app.mapper.TrainInvestigateMapper;
import com.app.model.ReturnHomeModel;
import com.app.model.SkillTrainingModel;
import com.app.model.TrainInvestigateModel;
import com.app.service.TrainInvestigateService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.vo.SkillTrainingVo;
import com.app.vo.TrainInvestigateVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 肃北县县域劳动力2024年补贴性职业技能培训意愿调查问卷
 * @Author: N
 * @Date: 2022/10/29 15:26
 */
@RestController
@RequestMapping("/")
public class TrainInvestigateController {
    @Autowired
    TrainInvestigateMapper trainInvestigateMapper;
    @Autowired
    TrainInvestigateService trainInvestigateService;

    @PostMapping("/insertTrainInvestigate")
    public Result<Object> insertTrainInvestigate(TrainInvestigateModel model
    ) {
        model.setCrtTime(CommonHelp.getNowTimestamp());
        trainInvestigateMapper.insert(model);
        return Result.success("操作成功");
    }

    /**
     * 问卷调查列表
     * @param model
     * @return
     * @throws ParseException
     */
    @GetMapping("/getTrainInvestigateList")
    public Result<Object> getTrainInvestigateList(TrainInvestigateModel model) throws ParseException {
        TrainInvestigateVo vo = trainInvestigateService.getPageByModel(model);
        return Result.success(vo);
    }

    /**
     * 导出
     * @param response
     * @param model
     * @return
     * @throws Exception
     */
    @PostMapping("/exportTrainInvestigate")
    @ResponseBody
    public void exportTrainInvestigate(HttpServletResponse response, @RequestBody TrainInvestigateModel model) throws Exception {
        model.setPage(1);
        model.setLimit(999999);
        TrainInvestigateVo vo = trainInvestigateService.getPageByModel(model);
        Map<String, Object> map=new HashMap<>();
        map.put("list",vo.getList());
        ClassPathResource classPathResource = new ClassPathResource("C:/upload/2024_sb_train.xlsx");
        String path = classPathResource.getPath();
        TemplateExportParams templateExportParams1 = new TemplateExportParams(path);
        Workbook wb = ExcelExportUtil.exportExcel(templateExportParams1, map);
        String fileName = "问卷花名册" + System.currentTimeMillis() + ".xlsx";
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            response.flushBuffer();
            ServletOutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
